<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingDevice;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Admin\Actions\Grid\DedustingDeviceHistory;
use App\Admin\Actions\Grid\SetRanges;


class DeviceController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('设备列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(DedustingDevice::with(['owner']), function (Grid $grid) {
            $grid->column('name', '设备名称');
            $grid->column('project', '所属项目');
            $grid->column('company', '所属公司');
            $grid->column('location', '地点');
            $grid->column('code', '编号');
            $grid->column('maintenance', '维保公司');
            $grid->column('sell_at', '出厂日期');
            $grid->column('maintain_at', '质保日期');
            $grid->column('machine_code', '维控机器码');
            $grid->column('qrcode','二维码')->image(100,100);
            $grid->column('owner.mobile','所属用户');
            $grid->column('status', '状态')->using(DedustingDevice::STATUS_TEXT);
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间');
            $grid->actions([new SetRanges()]);
            $grid->actions(new DedustingDeviceHistory());
        });
    }

    public function show($id, Content $content)
    {
        return $content->header('设备')
            ->description('详情')
            ->body(Show::make($id, DedustingDevice::with(['owner']), function (Show $show) {
                $show->field('name', '设备名称');
                
                $show->field('project', '所属项目');
                $show->field('company', '所属公司');
                $show->field('location', '地点');
                $show->field('code', '编号');
                $show->field('maintenance', '维保公司');
                $show->field('sell_at', '出厂日期');
                $show->field('maintain_at', '质保日期');
                $show->field('machine_code', '维控机器码');
                $show->field('qrcode','二维码')->image(100,100);
                $show->field('owner.mobile','所属用户');
                $show->field('status', '状态')->using(DedustingDevice::STATUS_TEXT);
                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');
            }));
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建设备')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new DedustingDevice(), function (Form $form) {
            $form->text('name', '设备名称')->required();;
            $form->text('project', '所属项目')->required();;
            $form->text('company', '所属公司')->required();;
            $form->text('location', '地点');
            $form->text('code', '编号')->required();
            $form->text('machine_code', '维控机器码')->required();
            $form->text('maintenance', '维保公司');
            
            $form->date('sell_at', '出厂日期')->default(null);
            $form->date('maintain_at', '质保日期')->default(null);

            $form->hidden('user_id')->value(Admin::user()->id);
            $form->hidden('qrcode');
            $form->radio('status', '状态')->options(DedustingDevice::STATUS_TEXT)->default(DedustingDevice::STATUS_ONLINE);

            $form->saving(function (Form $form) {
                $form->user_id = Admin::user()->id;

                $qrcode = DedustingDevice::genQrcode($form->code);
                $form->qrcode = $qrcode;
            });
            $form->deleted(function (Form $form, $result) {
                if ($result) {
                    $data = $form->model()->toArray();
                    DedustingDevice::delQrcode($data[0]['code']);
                }
            });

        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('设备编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }
}